<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div></div>
    <script>
        var div = document.querySelector('div')
        var timer = function() {
            var myDate = new Date() //创建时间日期对象
            var y = myDate.getFullYear()
            var m = (myDate.getMonth() + 1).toString().padStart(2, '0')
            var d = myDate.getDate().toString().padStart(2, '0')
            var h = myDate.getHours().toString().padStart(2, '0')
            var f = myDate.getMinutes().toString().padStart(2, '0')
            var s = myDate.getSeconds().toString().padStart(2, '0')
            var str = `${y}-${m}-${d} ${h}:${f}:${s}`
            div.innerHTML = str
        }
        timer()
        setInterval(timer, 1000)
    </script>
</body>

</html>